#!/bin/bash

BUNDLE=$1
INIT=$2

if [[ -n $INIT ]]; then
  echo "Enable modular log"
  hdc shell param set persist.ark.properties 0x200105c
  hdc shell reboot
  read -p "Waitting...... Press any key to continue after the device restart is complete"
fi

if ! [[ -n BUNDLE ]]; then
  echo "Bundle name is required."
  exit 1
fi

echo "Process ${BUNDLE}."
FILE="${BUNDLE}_redundant_file.txt"
if [[ -f $FILE ]]; then
  rm $FILE
fi

CMD="bm dump -n ${BUNDLE} | grep mainAbility | cut -d'"'"''"'"' -f4"
ABILITY=`hdc shell ${CMD}`
echo "start ${ABILITY}."

hdc shell aa start -a ${ABILITY} -b ${BUNDLE}
# Waitting for the application to start
sleep 3

hdc file recv data/app/el2/100/base/${BUNDLE}/files/${FILE}
if [[ -f $FILE ]]; then
  echo
  echo "--------------- Analysis Results ---------------"
  grep '^<----Summary---->' $FILE | cut -d'>' -f2
  echo "------------------------------------------------"
  grep '^unused file' $FILE
else
  echo "get $FILE failed."
fi

